package com.sea.physical.service.impl;


import com.sea.physical.entity.StockCreatePlan;
import com.sea.physical.entity.StockCreateRecord;
import com.sea.physical.mapper.StockCreatePlanMapper;
import com.sea.physical.service.StockCreatePlanService;
import com.sea.physical.service.StockCreateRecordService;
import com.codecrane.core.Constants;
import com.codecrane.core.power.entity.PowerUser;
import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.PhysicalStockStatusEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class StockCreatePlanServiceImpl implements StockCreatePlanService {

    @Autowired
    private StockCreatePlanMapper stockCreatePlanMapper;
    @Autowired
    private StockCreateRecordService stockCreateRecordService;

    @Override
    public List<StockCreatePlan> findByCnd(StockCreatePlan stockCreatePlan) {
        return stockCreatePlanMapper.findByCnd(stockCreatePlan);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(StockCreatePlan stockCreatePlan) throws Exception {
        stockCreatePlanMapper.insert(stockCreatePlan);
        StockCreateRecord stockCreateRecord=new StockCreateRecord();
        stockCreateRecord.setCreateId(stockCreatePlan.getId());
        //获取操作人信息
        Subject subject = SecurityUtils.getSubject();
        PowerUser user = (PowerUser) subject.getSession().getAttribute(Constants.CURRENT_USER_SESSION);
        if (null != user) {
            stockCreateRecord.setCreateOperateName(user.getUserNickname());
        }else{
            throw new Exception("暂未获取到用户信息");
        }
        stockCreateRecord.setCreateStatus(1);
        stockCreateRecord.setCreateOperateTime(DateUtils.getCurrentDateTime(DateUtils.DATE_FORMAT));
        stockCreateRecord.setCreateRemark(PhysicalStockStatusEnum.getStatusName(1));
        return stockCreateRecordService.save(stockCreateRecord);
    }

    @Override
    public StockCreatePlan findById(Integer id) {
        return stockCreatePlanMapper.findById(id);
    }

    @Override
    public int updateStockCreatePlan(Integer createId) {
        return stockCreatePlanMapper.updateStockCreatePlan(createId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int usave(StockCreatePlan stockCreatePlan) throws Exception {
        //获取操作人信息
        Integer createStatus = stockCreatePlan.getCreateStatus();

        return stockCreatePlanMapper.update(stockCreatePlan);
    }

    @Override
    public int delete(Integer id) {
        return stockCreatePlanMapper.delete(id);
    }

    @Override
    public Map<String, Integer> logisticsStatus() {
        return stockCreatePlanMapper.logisticsStatus();
    }
}
